package com.syh.jn.scm.infrastructure.repository.impl.supplier.finance;

import cn.redscarf.commons.wrapper.Wrapper;
import com.syh.core.ddd.infrastructure.repository.AbstractAggregateRepository;
import com.syh.jn.scm.domain.supplier.entity.finance.CostBill;
import com.syh.jn.scm.domain.supplier.repository.finance.CostBillRepository;
import com.syh.jn.scm.dto.supplier.finance.CostBillVo;
import com.syh.jn.scm.infrastructure.repository.model.finance.CostBillModel;
import com.syh.jn.scm.infrastructure.repository.utils.SQLAliasGenerateUtil;
import com.syh.jn.scm.infrastructure.wrapper.BaseResolve;
import com.syh.jn.scm.infrastructure.wrapper.ResolveToSQL;
import org.apache.ibatis.jdbc.SQL;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
public class CostBillRepositoryImpl extends AbstractAggregateRepository<CostBill, CostBillModel, Long> implements CostBillRepository {

  @Override
  protected BaseResolve getViewWrapperResolve(Wrapper wrapper) {
    Map<String,String> map = new HashMap<>();
    map.put("id","costBill.ID");
    map.put("customerType","costBill.CUSTOMER_TYPE");
    map.put("customerId","costBill.CUSTOMER_ID");
    map.put("code","costBill.CODE");
    map.put("state","costBill.STATE");
    map.put("confirmState","costBill.CONFIRM_STATE");
    map.put("inputTimestamp","costBill.INPUT_TIMESTAMP");
    map.put("createTimeStamp","costBill.CREATE_TIME_STAMP");
    map.put("businessEntityId", "costBill.BUSINESS_ENTITY_ID");
    map.put("statementOfAccountId", "costBill.STATEMENT_OF_ACCOUNT_ID");

    ResolveToSQL parse = new ResolveToSQL(wrapper, (propertyName) -> {
      return map.get(propertyName);
    });
    return parse;
  }

  @Override
  protected SQL getViewSql() {
    return new SQL(){{
      SELECT(
              SQLAliasGenerateUtil.aliasGenerate(CostBillModel.class,"costBill",null),
              "CONCAT(IFNULL(downstream.`NAME`, ''), IFNULL(logistics.`NAME`, ''), IFNULL(upstream.`NAME`, '')) AS name"
      );
      FROM("T_FINANCE_COST_BILL AS costBill");
      LEFT_OUTER_JOIN("T_BASE_DOWNSTREAM AS downstream ON costBill.CUSTOMER_ID = downstream.ID AND costBill.CUSTOMER_TYPE = 1");
      LEFT_OUTER_JOIN("T_BASE_UPSTREAM AS upstream ON costBill.CUSTOMER_ID = upstream.ID AND costBill.CUSTOMER_TYPE = 0");
      LEFT_OUTER_JOIN("T_BASE_LOGISTICS AS logistics ON costBill.CUSTOMER_ID = logistics.ID AND costBill.CUSTOMER_TYPE = 2");
    }};
  }


  @Override
  protected Class getViewObject() {
    return CostBillVo.class;
  }

  /**
   * 上传单据图片
   * @param bill
   */
  @Override
  public void upload(CostBill bill) {
    super.update(bill);
  }

  /**
   * 根据服务商id删除采购单
   * @param businessEntityId
   */
  @Override
  public void deleteByBusinessEntityId(Long businessEntityId) {
    String orderHql = "delete from CostBillModel where businessEntityId = :businessEntityId";
    Query orderQuery = this.hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(orderHql);
    orderQuery.setParameter("businessEntityId",businessEntityId);
    orderQuery.executeUpdate();
  }
}
